const effectStack: Array<any> = []

const breaker = new WeakMap()

function reactive(obj: any) {
  const handler = {
    get (target: any, prop: any, receiver: any) {
      console.log("//获取", receiver)
      const observer = effectStack.pop()
      if (observer) {
        breaker.set(target, observer)
      }
      
      return Reflect.get(target, prop, receiver)
    },
    set (target: any, prop: any, value: any) {
      let res = Reflect.set(target, prop,  value)
      let effect = breaker.get(target)
      if (effect) effect()
      return res
    }
  }

  let p = new Proxy(obj, handler)

  return p
}

function effect (fn: Function) {
  effectStack.push(fn)
  fn()
}

function main (): void {
  let obj = {name: 1}
  let p = reactive(obj)

  effect(() => {
    console.log(p.name)
  })

  p.name = 2
  p.name = 3
  p.name = 4
}

main()